package com.drophair.jspwork.bean;
import com.drophair.jspwork.testtools.Debug;

import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.Vector;
import java.security.MessageDigest;
public class SqlConnect {
    private static String driverName="com.mysql.cj.jdbc.Driver";
    private static String localHost="127.0.0.1";
    private static String mysqlPort="3306";
    private static String userName="root";
    private static String userPwd="123456";
    private static String dbBase="javadb";
    private static String url=String.format("jdbc:mysql://%s:%s/%s?useSSL=false&serverTimezone=UTC",localHost,mysqlPort,dbBase);

    private String sql="";

    public Vector<String> login(String uid,String upwd) throws ClassNotFoundException, SQLException {
        Vector<String> info = new Vector<String>();
        int sign = 0;
        Class.forName(driverName);
        Connection conn = DriverManager.getConnection(url, userName, userPwd);
        System.out.println(url);
        Statement st = conn.createStatement();
        System.out.println(uid);
        System.out.println(upwd);
        sql = String.format("select Uname,Uwho from userinfo where Uname= '%s' and Upasswd='%s'", uid, upwd);
        System.out.println("sql语法是：" + sql);
        ResultSet resultSet = st.executeQuery(sql);
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        while (resultSet.next()) {
            Debug.consle_print("循环检查" + resultSetMetaData.getColumnCount());
            for (int idx = 1; idx <= resultSetMetaData.getColumnCount(); idx++) {
                Debug.consle_print("当前idx为：" + Integer.toString(idx));
                info.add(resultSet.getString(idx));
            }
        }
        conn.close();
        return info;
    }

    public Vector<String> register(String uname,String upwd) throws SQLException, ClassNotFoundException, NoSuchAlgorithmException {
        // isReg，是否注册成功，默认为否，注册成功为true
        Vector<String> vector=new Vector<String>();
        boolean isReg=false;
        sql="select count(*) from userinfo;";
        Class.forName(driverName);
        Connection connection=DriverManager.getConnection(url,userName,userPwd);
        Statement statement=connection.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);
        int idx=0;
        if(resultSet.next()){
            idx=Integer.parseInt(resultSet.getString(1));
        }
        MessageDigest messageDigest=MessageDigest.getInstance("md5");
        byte[] bytes = messageDigest.digest(upwd.getBytes());
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes){
            int bt = b&0xff;
            if (bt < 16){
                stringBuffer.append(0);
            }
            stringBuffer.append(Integer.toHexString(bt));
        }
        String pwd_md5= stringBuffer.toString();
        sql=String.format("insert into userinfo values(%d,'%s',%d,'%s','%s','%s');",idx+1,uname,1,upwd,pwd_md5,"");
        int operation=0;
        operation=statement.executeUpdate(sql);
        if(operation>0){
            isReg=true;
        }
        connection.close();
        if(isReg==true){
            vector.add(Integer.toString(idx+1));
            vector.add(uname);
        }
        connection.close();
        return vector;
    }
    public void selectAll() throws ClassNotFoundException, SQLException {
        Class.forName(driverName);
        Connection conn = DriverManager.getConnection(url,userName, userPwd);
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select*from userinfo");
        int idx=0;
        while (rs.next()){
            System.out.println(idx++);
        }
        conn.close();
    }

    public String getNowName(String userid) throws SQLException, ClassNotFoundException {
        Class.forName(driverName);
        Connection connection=DriverManager.getConnection(url,userName,userPwd);
        Statement statement=connection.createStatement();
        sql=String.format("select Uname from userinfo where Userid=%s;",userid);
        System.out.println("获取用户名的语句是："+sql);
        ResultSet  resultSet=statement.executeQuery(sql);
        String nowName="";
//        nowName=resultSet.getNString(0);
        while (resultSet.next()){
            nowName=resultSet.getString(1);
        }
        connection.close();
        return nowName;
    }
}
